package cn.smbms.controller;

import cn.smbms.dao.WorkingMapper;
import cn.smbms.pojo.Working;
import cn.smbms.service.WorkingService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/working")
public class WorkingController {
    private static Logger logger=Logger.getLogger(WorkingController.class);

    @Resource
    private WorkingMapper workingMapper;
    @Resource
    private WorkingService workingService;

    @RequestMapping(value = "/list")
    public String getqueryAll(Model model){
        List<Working> list = workingMapper.queryAll();
        //将信息保存到model中
        if (list != null) {
            model.addAttribute("list", list);
        }
        return "list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(@ModelAttribute("working") Working working) {//
        logger.info("跳转到新增页面");
        return "add";
    }

    @RequestMapping(value = "/workingaddsave", method = RequestMethod.POST)
    public String addSave(Working working, HttpSession session) {
        working.setCreateDate(new Date());
        try {
            if (workingService.add(working)){
                return "redirect:/working/list";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "add";
    }

    @RequestMapping(value = "/detailsWorking",method = RequestMethod.GET)
    @ResponseBody
    public Working detailsWorking(@RequestParam(value = "id")String id){
        logger.debug("detailsWorking id:"+id);

        String cjson="";

        Working working=new Working();

        try {
            working=workingService.getWorkingById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return working;
    }

}
